﻿using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.BLL;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Reflection;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using System.Data;
using IPR_StudyMeetingMrgSys.ViewModel;
using Newtonsoft.Json;
using System.Text;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class ExamResultController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.T_ExamResult  bller = new BLL.T_ExamResult();
        BLL.T_ExamResults bllers = new T_ExamResults();
        BLL.SysManage bllsm = new SysManage();

        #region 交卷操作
        /// <summary>
        /// 正常交卷
        /// </summary>
        /// <param name="list">答题详情</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Exam/SubmitExam")]
        public IHttpActionResult SubmitExam(VM_ExamResult vmer)
        {
            return SubmitExam(vmer, false);
        }
        /// <summary>
        /// 异常交卷
        /// </summary>
        /// <param name="list">答题详情</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Exam/SubmitExceptionExam")]
        public IHttpActionResult SubmitExceptionExam(VM_ExamResult list)
        {
            return SubmitExam(list, true);
        }

        #region 私有方法
        /// <summary>
        /// 交卷操作
        /// </summary>
        /// <param name="list">答题详情</param>
        /// <param name="isExceptionExam">是否异常交卷</param>
        /// <returns></returns>
        private IHttpActionResult SubmitExam(VM_ExamResult vmer,bool isExceptionExam)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                long maxid = bllsm.GetMaxId("ERID", "T_ExamResult");
                Model.T_UserInfo mui = new T_UserInfo().GetModel(" UserID='" + vmer.UserID + "' ");
                Model.T_ExamResult modeler = new Model.T_ExamResult()
                {
                    UserID = vmer.UserID,
                    UserNo = mui.UserNo,
                    UserSFZH = mui.UserSFZH,
                    TrainID = vmer.TrainID,
                    Score = vmer.Score,
                    VedioUrl = vmer.VedioUrl,
                    RefScore = vmer.RefScore,
                    StartDateTime = vmer.StartDateTime,
                    EndDateTime = vmer.EndDateTime,
                    ProNo = vmer.ProNo,
                    ERID = maxid,
                    ExamData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                List<Model.T_ExamResults> listsub = new List<Model.T_ExamResults>(); 
                 
                foreach (var item in vmer.ExamResultSub)
                {
                    bllers.Add(new Model.T_ExamResults()
                    {
                        ERID=maxid,
                        ExamPoint=item.ExamPoint,
                        AppraisalOrder=item.AppraisalOrder,
                        AppraisalValue=item.AppraisalValue,
                        Score=item.Score
                    });
                }
                bller.Add(modeler);
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "交卷成功";
                resultMsg.Count = 1;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        #endregion
        #endregion
    }
}
